package com.ruoyi.mall.product.domain.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.core.constant.ProductConstant;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.mall.product.domain.entity.PmsAttrAttrgroupRelation;
import com.ruoyi.mall.product.domain.entity.PmsAttrGroup;
import com.ruoyi.mall.product.domain.entity.PmsCategory;
import com.ruoyi.mall.product.domain.repository.*;
import com.ruoyi.mall.product.domain.service.IPmsAttrService;
import com.ruoyi.mall.product.domain.service.IPmsCategoryService;
import com.ruoyi.mall.product.domain.vo.PmsAttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.product.domain.entity.PmsAttr;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品属性Service业务层处理
 *
 * @author zhangqishu
 * @date 2022-05-25
 */
@Service
public class PmsAttrServiceImpl implements IPmsAttrService {
    @Autowired
    private PmsAttrMapper pmsAttrMapper;

    @Autowired
    private PmsAttrGroupMapper pmsAttrGroupMapper;

    @Autowired
    private PmsCategoryMapper pmsCategoryMapper;

    @Autowired
    private PmsAttrAttrgroupRelationMapper pmsAttrAttrgroupRelationMapper;
    @Autowired
    private IPmsCategoryService pmsCategoryService;

    /**
     * 查询商品属性
     *
     * @param id 商品属性主键
     * @return 商品属性
     */
    @Override
    public PmsAttr selectPmsAttrById(Long id) {
        //获取分类数据set进去
        PmsAttr pmsAttr = pmsAttrMapper.selectPmsAttrById(id);
        Long[] path = pmsCategoryService.findCategoryPath(pmsAttr.getCategoryId());
        pmsAttr.setCategoryPath(path);

        return pmsAttr;
    }

    /**
     * 查询商品属性列表
     *
     * @param pmsAttr  商品属性
     * @param attrType 属性类型
     * @return 商品属性
     */
    @Override
    public List<PmsAttr> selectPmsAttrList(PmsAttr pmsAttr, String attrType) {
        QueryWrapper<PmsAttr> queryWrapper = new QueryWrapper<>();
        if (ProductConstant.AttrEnum.ATTR_TYPE_SALE.getMsg().equalsIgnoreCase(attrType)) {
            queryWrapper.eq("type", ProductConstant.AttrEnum.ATTR_TYPE_BASE.getMsg().equalsIgnoreCase(attrType) ? 1 : 0);
        }

        String key = pmsAttr.getKey();
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and(i -> i.eq("id", key).or().like("name", key));
        }
        Long categoryId1 = pmsAttr.getCategoryId();
        if (categoryId1!=null&&categoryId1==0) {
            List<PmsAttr> pmsAttrs = pmsAttrMapper.selectList(queryWrapper);
            for (PmsAttr attr : pmsAttrs) {
                if (ProductConstant.AttrEnum.ATTR_TYPE_BASE.getMsg().equalsIgnoreCase(attrType)) {
                    PmsAttrGroup pmsAttrGroup = pmsAttrGroupMapper.selectById(attr.getGroupId());
                    attr.setGroupName(pmsAttrGroup.getName());
                }
                PmsCategory pmsCategory = pmsCategoryMapper.selectById(attr.getCategoryId());
                attr.setCategoryName(pmsCategory.getName());
            }
            return pmsAttrs;
        } else {
            Long categoryId = pmsAttr.getCategoryId();

            queryWrapper.eq("category_id", categoryId);
            //select * from pms_attr_group where category_id =? and (id=key or name like %key%)
            List<PmsAttr> pmsAttrs = pmsAttrMapper.selectList(queryWrapper);
            for (PmsAttr attr : pmsAttrs) {
                if (ProductConstant.AttrEnum.ATTR_TYPE_BASE.getMsg().equalsIgnoreCase(attrType)) {
                    PmsAttrGroup pmsAttrGroup = pmsAttrGroupMapper.selectById(attr.getGroupId());
                    attr.setGroupName(pmsAttrGroup.getName());
                }
                PmsCategory pmsCategory = pmsCategoryMapper.selectById(attr.getCategoryId());
                attr.setCategoryName(pmsCategory.getName());
            }
            return pmsAttrs;
        }
    }

    /**
     * 新增商品属性
     *
     * @param pmsAttrVo 商品属性
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPmsAttr(PmsAttrVo pmsAttrVo) {
        int i = 0;
        PmsAttr pmsAttr = new PmsAttr();
        BeanUtils.copyProperties(pmsAttrVo, pmsAttr);
        i = pmsAttrMapper.insertPmsAttr(pmsAttr);

        //保存关联关系  如果为基本属性才保存关联表
        if (pmsAttrVo.getType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()&&pmsAttrVo.getGroupId()!=null) {
            PmsAttrAttrgroupRelation relation = new PmsAttrAttrgroupRelation();
            relation.setAttrId(pmsAttr.getId());
            relation.setAttrGroupId(pmsAttrVo.getGroupId());
            i = pmsAttrAttrgroupRelationMapper.insert(relation);
        }

        return i;
    }

    /**
     * 修改商品属性
     *
     * @param pmsAttrVo 商品属性
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePmsAttr(PmsAttrVo pmsAttrVo) {

        PmsAttr pmsAttr = new PmsAttr();
        BeanUtils.copyProperties(pmsAttrVo, pmsAttr);
        int i = pmsAttrMapper.updatePmsAttr(pmsAttr);

        //修改分组关联 如果为基本属性才修改或保存关联表
        if (pmsAttrVo.getType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) {
            PmsAttrAttrgroupRelation relation = new PmsAttrAttrgroupRelation();
            relation.setAttrGroupId(pmsAttrVo.getGroupId());
            relation.setAttrId(pmsAttrVo.getId());
            UpdateWrapper<PmsAttrAttrgroupRelation> wrapper = new UpdateWrapper<>();
            wrapper.eq("attr_id", pmsAttrVo.getId());

            //判断关联表中有没有当前记录，有就是修改，没有就新增
            Integer count = pmsAttrAttrgroupRelationMapper.selectCount(new QueryWrapper<PmsAttrAttrgroupRelation>()
                    .eq("attr_id", pmsAttrVo.getId()));
            if (count > 0) {
                return pmsAttrAttrgroupRelationMapper.update(relation, wrapper);

            } else {
                return pmsAttrAttrgroupRelationMapper.insert(relation);
            }
        }

        return i;
    }

    /**
     * 批量删除商品属性
     *
     * @param ids 需要删除的商品属性主键
     * @return 结果
     */
    @Override
    public int deletePmsAttrByIds(Long[] ids) {
        return pmsAttrMapper.deletePmsAttrByIds(ids);
    }

    /**
     * 删除商品属性信息
     *
     * @param id 商品属性主键
     * @return 结果
     */
    @Override
    public int deletePmsAttrById(Long id) {
        return pmsAttrMapper.deletePmsAttrById(id);
    }

    /**
     * 修改商品属性状态
     *
     * @param pmsAttr
     * @return
     */
    @Override
    public int updateStatusPmsAttr(PmsAttr pmsAttr) {
        return pmsAttrMapper.updateById(pmsAttr);
    }
}
